home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14291 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: invincible.astro.uu.se!daniel
  2. From: daniel@invincible.astro.uu.se (Daniel Kallander)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Reverting of big array ?
  5. Date: 29 Mar 1996 18:24:04 GMT
  6. Organization: Uppsala University
  7. Distribution: world
  8. Message-ID: <4jh9s4$238k@columba.udac.uu.se>
  9. References: <Pine.SOL.3.91.960326132111.6385A-100000@evolution> <3158894A.446B@lmf-di.puc-rio.br>
  10. NNTP-Posting-Host: invincible.astro.uu.se
  11.  
  12. In article <3158894A.446B@lmf-di.puc-rio.br>, Paulo Eduardo Neves <neves@lmf-di.puc-rio.br> writes:
  13. |> Thomas wrote:
  14. |> > 
  15. |> > I need to revert an array (of chars).
  16. |> > I used the folowing code:( l=length of array)
  17. |> > 
  18. |> >   char *rev=new char[l+1];
  19. |> >   for(i=l-1,j=0;i>=0;i--,j++)  rev[j]=seq[i];
  20. |> >   rev[j]='\0';
  21. |> > 
  22. |> > this code works, but is terribly slow, when using an string with
  23. |> > size ~ 6000.
  24. |> > doing the same with the unix command 'rev' takes 1/2 second.
  25. |> > 
  26. |> > Any ideas  ???
  27. |> > 
  28. |> > -thomas
  29. |> > 
  30. |> > Sicheritz Ponten Thomas E.              UPPSALA UNIVERSITY
  31. |> 
  32.  
  33. Don't use chars, use integers of the same bit length as the number
  34. of bits that your processor uses internally, which should be the
  35. same as the C 'int' type. Because the byte length of your character
  36. array probably isn't divisible with the number of bytes in an
  37. 'int', you have to be careful at the endpoints. In that case,
  38. it is probably easiest to just revert a few more bytes than
  39. necessary.
  40. This should make a substantial difference.
  41.  
  42. Moreover, you don't really need more than one of your two counters.
  43. j is a simple function of i, and thus unnecessary.
  44. -- 
  45. -------------------------------------------------------------
  46. Daniel Kallander, e-mail:daniel@astro.uu.se
  47. Uppsala Astronomical Observatory
  48. Theoretical Astrophysics
  49. SWEDEN
  50. -------------------------------------------------------------
  51.